//
//  TicketBusinessDetailController.m
//  ELife
//
//  Created by xiaozhi on 11-7-23.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "TicketBusinessDetailController.h"
#import "UIViewUtil.h"
#import "ELifeAppDelegate.h"
#import "TicketBusinessModel.h"
#import "GetMyData.h"
#import "LoginController.h"
#define OFFSET_Y 7

@implementation TicketBusinessDetailController
@synthesize timeButton, priceButton, amountButton, contentLable, contentImage;

- (id)initWithTicketBusinessModel:(TicketBusinessModel *)model
{
    self = [super initWithNibName:@"TicketBusinessDetailController" bundle:nil];
    if (self) {
        _detailModel = [model retain];
    }
    
    return self;
}

- (void)dealloc
{
    [contentImage release];
    [_timeArray release];
    [_priceArray release];
    [contentLable release];
    [amountButton release];
    [timeButton release];
    [priceButton release];
    [_detailModel release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)initChildViewFrame:(UIView *)childView withX:(NSInteger)x withWid:(NSInteger)wid;
{
    [UIViewUtil viewRoundFrame:childView];
    
    NSInteger childFrameHei = childView.frame.size.height;
    
    childView.frame = CGRectMake(x, _detailY, wid, childFrameHei);
    
    _detailY += childFrameHei;
    _detailY += OFFSET_Y;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
        
    // init detail views
    UIScrollView *scrollView = (UIScrollView *)self.view;
    scrollView.backgroundColor = [UIViewUtil getCommonBg];
    
    UIView *view1 = [scrollView viewWithTag:1];
    UIView *view2 = [scrollView viewWithTag:2];
    UIView *view3 = [scrollView viewWithTag:3];
    UIView *view4 = [scrollView viewWithTag:4];
    UIView *view5 = [scrollView viewWithTag:5];
    const NSInteger COMMON_X = 20;
    const NSInteger COMMON_WID = 280;
    
    _detailY = OFFSET_Y;
    
    [self initChildViewFrame:view1 withX:COMMON_X withWid:COMMON_WID];
    [self initChildViewFrame:view2 withX:COMMON_X withWid:COMMON_WID];
    
    [self initChildViewFrame:view3 withX:COMMON_X withWid:COMMON_WID];
    [self initChildViewFrame:view4 withX:COMMON_X withWid:COMMON_WID];
    [self initChildViewFrame:view5 withX:COMMON_X withWid:COMMON_WID];

    [scrollView setContentSize:CGSizeMake(COMMON_WID, _detailY)];
    
    // fill content
    UILabel *timeLabel = (UILabel *)[view1 viewWithTag:101];
    UILabel *addressLabel = (UILabel *)[view1 viewWithTag:102];
    UILabel *priceLabel = (UILabel *)[view1 viewWithTag:103];
    UILabel *titleLabel = (UILabel *)[view1 viewWithTag:104];
    UIImageView *imageView = (UIImageView *)[view1 viewWithTag:105];
    
    // request img
    imageView.image = [[UIImage alloc] init];
    curImageView = (UIImageView *)[self.view viewWithTag:105];
    [[[ELifeAppDelegate App] httpRequestPoint]getImageDataByUrl:_detailModel.goods_client_smallpic forUIImage:imageView.image delegate:self];
    

    NSString *timeAndAddress = _detailModel.ticket_time;
    NSArray *timeAndAddressArray = [timeAndAddress componentsSeparatedByString:@"|"];
    NSString *time = [timeAndAddressArray objectAtIndex:0];
    NSString *address = [timeAndAddressArray objectAtIndex:1];
    NSString *title = _detailModel.goods_name;

    [titleLabel setText:title];
    [timeLabel setText:time];
    [addressLabel setText:address];
    [priceLabel setText:@"无"];
    
    
    NSString *brief = _detailModel.goods_brief;

    brief = [NSString stringWithFormat:@"项目介绍:%@", brief];
    
    UILabel *briefLabel = (UILabel *)[view3 viewWithTag:107];
    [briefLabel setText:brief];
    
   _priceArray = [_detailModel.goods_price componentsSeparatedByString:@","];
    [_priceArray retain];
    
    // 先去掉地址
    NSArray *timeList = [_detailModel.ticket_time componentsSeparatedByString:@","];
    NSInteger lastIndex = [timeList count] - 1;
    NSString *lastTime = [timeList objectAtIndex:lastIndex];
    NSArray *tempArr = [lastTime componentsSeparatedByString:@"|"];
    tempArr = [tempArr objectAtIndex:0];
    _timeArray = [[NSMutableArray alloc] initWithArray:timeList];
    [_timeArray removeLastObject];
    [_timeArray addObject:tempArr];
    [_timeArray retain];
    
    
    [timeButton setTitle:[_timeArray objectAtIndex:0] forState:UIControlStateNormal];
    [priceButton setTitle:[_priceArray objectAtIndex:0] forState:UIControlStateNormal];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)add
{
    NSInteger amount = [amountButton.titleLabel.text intValue];
    amount++;
    [amountButton setTitle:[NSString stringWithFormat:@"%d", amount] 
                  forState:UIControlStateNormal];
}

- (void)reduce
{
    NSInteger amount = [amountButton.titleLabel.text intValue];
    if (amount <= 1) {
        return;
    }
    
    amount--;
    [amountButton setTitle:[NSString stringWithFormat:@"%d", amount] 
                  forState:UIControlStateNormal];
}

- (void)pay
{
    
}

- (void)selectPrice
{
    UIActionSheet *priceActionSheet = [[UIActionSheet alloc] initWithTitle:@"请选择价格" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];
    
    
    for (NSString *price in _priceArray) {
        [priceActionSheet addButtonWithTitle:price];
    }
    
    [priceActionSheet showInView:self.view];
    [priceActionSheet release];
    
    _actionSheetListener = priceButton;
}

- (void)selectTime
{
    UIActionSheet *timeActionSheet = [[UIActionSheet alloc] initWithTitle:@"请选择价格" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];
    
                                    
    for (NSString *time in _timeArray) {
        [timeActionSheet addButtonWithTitle:time];
    }
    
    [timeActionSheet showInView:self.view];
    [timeActionSheet release];
    _actionSheetListener = timeButton;
}

- (void)refreshWithImageData:(NSData *)imgData
{
    UIImageView *imgView = curImageView;
    imgView.image = [[UIImage alloc] initWithData:imgData];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [_actionSheetListener setTitle:[actionSheet buttonTitleAtIndex:buttonIndex] forState:UIControlStateNormal];
}

- (void)segmentChange:(id)sender
{
    UISegmentedControl *segmentControl = (UISegmentedControl *)sender;
    NSInteger focusIndex = [segmentControl selectedSegmentIndex];
    
    [contentLable setHidden:NO];
    [contentImage setHidden:YES];
    switch (focusIndex) {
        case 0:
            [contentLable setText:_detailModel.goods_brief];
            break;
        case 1:
            if ([_detailModel.goods_desc length] == 0) {
                [contentLable setText:@"暂无内容"];
            } else {
                [contentLable setText:_detailModel.goods_desc];

            }
            break;
        case 2:
            curImageView = contentImage;
            contentImage.image = [UIImage alloc];
            [[[ELifeAppDelegate App] httpRequestPoint] getImageDataByUrl:_detailModel.goods_pics forUIImage:contentImage.image delegate:self];
            [contentLable setHidden:YES];
            [contentImage setHidden:NO];
            break;
        case 3:
            [contentLable setText:@"暂无内容"];
            break;
        default:
             [contentLable setText:@""];
            break;
    }
    
}

- (void)requestShoppingCar
{
    NSString* userid = [GetMyData getUserId];
    ELifeAppDelegate *app = [ELifeAppDelegate App];
    
    NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"%@goods_order.php?", [[NSBundle mainBundle] localizedStringForKey:(@"BussinessHost") value:@"" table:nil]]];
    
    NSMutableDictionary *param = [[NSMutableDictionary alloc]initWithCapacity:4];
    [param setObject:@"add_goodsorder" forKey:@"action"];
    [param setObject:userid forKey:@"user_id"];
    [param setObject:_detailModel.goods_id forKey:@"goods_id"];
    [param setObject:amountButton.titleLabel.text forKey:@"goods_number"];
    //    [param setObject:[password text] forKey:@"property_id"];
    
    
    [param setObject:[[NSBundle mainBundle] localizedStringForKey:(@"ClientVersion") value:@"" table:nil] forKey:@"version"];
    
    NSDictionary* dic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:url, @"add_goodsorder:", nil] forKeys:[NSArray arrayWithObjects:@"url", @"requestMethod", nil]];
    
    [[app httpRequestPoint]openUrl:dic postParam:param target:self];
}

- (void)addToShoppingCar
{
    NSString* userid = [GetMyData getUserId];
    
    if (userid == nil) {
        LoginController* lc = [[LoginController alloc] init:self];
        [self.navigationController pushViewController:lc animated:YES];
        [lc release];
        return;
    }
    
    [self requestShoppingCar];

}

- (void)add_goodsorder:(NSData *)response
{    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"恭喜" message:@"添加到购物车成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alertView show];
    [alertView release];
}

- (void) registerOver:(NSString *)text
{
    [self requestShoppingCar];
}

@end
